using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace _11
{
    public partial class Form1 : Form
    {
        List<string> wordList = new List<string>();
        List<string> meaningList = new List<string>();
        private string currentMeaning;
        private string currentWord;
        private int current = 0;
        public Form1()
        {
            InitializeComponent();
            InitializeDatabaseConnection();
            Initialize();
        }

        private void Initialize()
        {
            label1.Text = meaningList[current];
        }
        private void InitializeDatabaseConnection()
        {
            MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder();
            
            //数据库连接时的用户名，可以用pid
            builder.UserID = "root";
            //数据库连接时的密码，可以用pwd
            builder.Password = "Password";
            //数据库连接时的服务器地址
            builder.Server = "localhost";
            //要连接的数据库
            builder.Database = "vocabulary";

            var connection = new MySqlConnection(builder.ConnectionString);
            

            try
            {
                connection.Open();
            }
            catch (MySqlException ex)
            {
                // 处理连接失败的情况，例如记录日志或者显示错误消息
                MessageBox.Show("连接失败：" + ex.Message);
            }
            
            string query = "SELECT * FROM words";
    
            MySqlCommand command = new MySqlCommand(query, connection);
            try
            {
                MySqlDataReader reader = command.ExecuteReader();
                
                
                while (reader.Read())
                {
                    // 读取每一行数据并存储在数组中
                    string word = reader.GetString("word");
                    string meaning = reader.GetString("meaning");
                    // 读取其他列的数据...
            
                    // 存储数据到数组
                    wordList.Add(word);
                    meaningList.Add(meaning);
                    
                }
        
                reader.Close();
        
                // 现在你可以在数组中获取读取到的数据，例如 idArray[0] 表示第一行的 id 数据
            }
            catch (MySqlException ex)
            {
                // 处理查询过程中出现的异常，例如记录日志或者显示错误消息
                MessageBox.Show("查询失败：" + ex.Message);
            }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == wordList[current])
            {
                if (current < 2)
                {
                    MessageBox.Show("正确");
                    current++;
                    Initialize();
                }
                else
                {
                    MessageBox.Show("恭喜你学习完所有单词");
                }
            }
            else
            {
                MessageBox.Show("回答错误");
            }
        }
    }
}